//
//  SCStarScoreView.m
//  Shuaiche
//
//  Created by 党帅 on 2019/9/5.
//  Copyright © 2019 ShuaiChe. All rights reserved.
//

#import "SCStarScoreView.h"

@interface SCStarScoreView ()
@property (nonatomic, strong) UIView *foregroundView;
@property (nonatomic, strong) UIView *backgroundView;
@end

@implementation SCStarScoreView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _editEnable = YES;
        _itemSpace = 15;
        _itemWidth = 25;
        _numberOfItems = 5;
        _normalImageName = @"star_score_0";
        _highlightImageName = @"star_score_1";
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    _backgroundView = [self createStarViewWithImage:_normalImageName];
    _backgroundView.frame = self.bounds;
    [self addSubview:_backgroundView];
}

- (void)setItemWidth:(CGFloat)itemWidth {
    if (_itemWidth == itemWidth) {
        return;
    }
    _itemWidth = itemWidth;
    [_backgroundView removeFromSuperview];
    _backgroundView = [self createStarViewWithImage:_normalImageName];
    _backgroundView.frame = self.bounds;
    [self addSubview:_backgroundView];
}

- (void)setItemSpace:(CGFloat)itemSpace {
    if (_itemSpace == itemSpace) {
        return;
    }
    _itemSpace = itemSpace;
    [_backgroundView removeFromSuperview];
    _backgroundView = [self createStarViewWithImage:_normalImageName];
    _backgroundView.frame = self.bounds;
    [self addSubview:_backgroundView];
}

- (void)starButtonClick:(UIButton *)sender {
    if (_editEnable) {
        self.score = (float)(sender.tag - 1000);
        if ([_delegate respondsToSelector:@selector(starScoreViewValueChanged:)]) {
            [_delegate starScoreViewValueChanged:_score];
        }
    }
}

- (void)setScore:(CGFloat)score {
    _score = score;
    [self updateForeground];
}

- (void)updateForeground {
    
    float upper = ceilf(_score);
    float lower = floorf(_score);
    float w = (_itemWidth + _itemSpace) * lower;
    if (upper > lower) {
         w = w + (_score - lower) * _itemWidth;
    }
    self.foregroundView.frame = CGRectMake(0, 0, w, self.bounds.size.height);
}

- (UIView *)createStarViewWithImage:(NSString *)imageName {
    UIView *view = [[UIView alloc] init];
    float y =  (CGRectGetHeight(self.frame) - _itemWidth) /2.f;
    for (NSInteger i = 0; i < self.numberOfItems; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * _itemWidth + i * _itemSpace, y, _itemWidth, _itemWidth);
        button.tag = 1001 + i;
        [button setBackgroundImage:[UIImage imageNamed:_normalImageName] forState:0];
        [button addTarget:self action:@selector(starButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
    }
    return view;
}

- (UIView *)foregroundView {
    if (!_foregroundView) {
        _foregroundView = [[UIView alloc] init];
        _foregroundView.userInteractionEnabled = NO;
        float y =  (CGRectGetHeight(self.frame) - _itemWidth) /2.f;
        for (NSInteger i = 0; i < self.numberOfItems; i ++) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_highlightImageName]];
            imageView.frame = CGRectMake(i * _itemWidth + i * _itemSpace, y, _itemWidth, _itemWidth);
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [_foregroundView addSubview:imageView];
        }
        _foregroundView.clipsToBounds = YES;
        [self addSubview:_foregroundView];
    }
    return _foregroundView;
}

@end
